#include chip.inc
#include vars.inc
#include circ_buffer.inc
	code
;-----------------------------------------------------------------;
;Begin INITS routine
;	Initialises the PIC's hardware
;	Prerequisites: 	None
;	Input:			None
;	Output:			None
;	Cycles:			?
;	Bank change:	No
INITS
	global INITS
	
INIT_OSC:
	MOVLW 	0xF0
	MOVWF 	OSCCON,A ;INITIALISE OSCILLATOR TO RUN AT 64MHz (IF USING INTERNAL OSCILLATOR)

INIT_UART:	
	;Initialise UART buffer
	call	Buffer_Init
	;Initialise UART for 2 MBAUD TX
	CLRF 	SPBRGH
	MOVLW 	0x07
	MOVWF 	SPBRG,A
	MOVLW 	0x08
	MOVWF 	BAUDCON,A
	MOVLW 	0x26
	MOVWF 	TXSTA,A
	MOVLW 	0x90
	MOVWF 	RCSTA,A
	BCF 	TRISA,7,A ;SET TX PORT AS OUTPUT
	
INIT_SPI:
	;INITIALISE SPI MODULE FOR DATA CAPTURE
;	BSF		SSPSTAT,CKE,A
	MOVLW	0x15
	MOVWF	SSPCON1,A
	
INIT_VARIABLES:
	;INITIALISE INTERNAL VARIABLES
	CLRF	LOOK_FOR_ACK_NACK,A
	CLRF	CLOCK_HAS_GONE_LOW,A
	BTFSC 	SPORT,SDAT,A
	BTFSC 	SPORT,SCLK,A
	MOVLW	0xF9
	MOVWF	TRISC,A
	MOVWF	LATC,A
;	call	tests
INIT_INTERRUPTS:
	;INITIALISE INTERRUPTS
	BCF		ANSELH,ANS10,A ;ENABLE RB4 DIGITAL BUFFER
	CLRF	WPUB,A
	BSF		IOCB,IOCB4,A
	BCF		IOCB,IOCB6,A
	MOVLW	0xFF
	MOVWF	RCON,A
	MOVLW	0xC8
	MOVWF	INTCON,A
		
	return
	
tests:
BASE EQU	0X00

	MOVLW	0x00
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x01
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x02
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x03
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x04
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x05
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x06
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x07
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x08
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x09
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x0A
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x0B
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x0C
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x0D
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x0E
	IORLW	BASE
	CALL	sendCharHEAD
	MOVLW	0x0F
	IORLW	BASE
	CALL	sendCharHEAD
	return

sendChar:	
	MOVWF	BUFFER_PUSH_DATA,A
	;NOP
	MOVLW	UART_TX_CHAR_DATA
	MOVWF	BUFFER_PUSH_ACTION,A

	BCF INTCON,GIE,A						;DISABLE INTERRUPTS
	CALL	Buffer_Push					;AND SEND IT
	BSF INTCON,GIE,A						;RE-ENABLE INTERRUPTS
	return
sendCharHEAD:	
	MOVWF	BUFFER_PUSH_DATA,A
	;NOP
	MOVLW	UART_TX_CHAR_HEAD
	MOVWF	BUFFER_PUSH_ACTION,A

	BCF INTCON,GIE,A						;DISABLE INTERRUPTS
	CALL	Buffer_Push					;AND SEND IT
	BSF INTCON,GIE,A						;RE-ENABLE INTERRUPTS
	return
	end
